import FrameAnimmationBase from "../lib/FrameAnimationBase";
import LineImgEff2 from "../lib/LineImgEff2";
import MathTool from "../lib/MathTool";

export default class AnimManager{
    public static BombAnim:string="BombAnim";
    public static BombSpAnim:string="BombSpAnim";
    public static lineAnim:string="lineAnim";
    public static show(_parent:any,_x:number,_y:number,_type:string,_func:Function=null,_time:number=500){
        this.getAnim(_type,(_anim:any)=>{
            _anim.pos(_x,_y);
            _anim.play();
            _parent.addChild(_anim);
            Laya.timer.once(_time,this,(_anim)=>{
                _anim.removeSelf();
                Laya.Pool.recover(_anim.name,_anim);
                if(_func)_func();
            },[_anim])
        });
    }

    public static showRowLineEff(_parent:any,_x:number,_y:number){
        let teff:LineImgEff2= Laya.Pool.getItem(LineImgEff2.FLAG);
        if(!teff)teff=new LineImgEff2("main/lightining-blue-test.png",128,128);
        teff.play(_x,_y,_x,660-_x);
        _parent.addChild(teff);
    }

    public static showTEff(_parent:any,_x:number,_y:number,_arr:any,_func:Function=null,_time:number=500){
        for(let i=0;i<_arr.length;i++){
            let timg=Laya.Pool.getItemByCreateFun("TEImgs",this.createTEImg,this);
            timg.pos(_x,_y);
            _parent.addChild(timg);
            let tangle = MathTool.getAngleByTwoPoint(_x,_y,_arr[i].x,_arr[i].y);
            Laya.Tween.to(timg,{x:_arr[i].x,y:_arr[i].y,rotation:tangle},_time,Laya.Ease.circIn,Laya.Handler.create(this,(_img)=>{
                _img.removeSelf();
                Laya.Pool.recover("TEImgs",_img);
                if(_func)_func();
            },[timg]))
        }
    }

    public static showLineImgEff(_parent:any,_x:number,_y:number,_angle:number,_len:number=1000,_func:Function=null,_time:number=400){
        let timg:Laya.Image=Laya.Pool.getItemByCreateFun("lineimg",this.createLineImg,this);
        timg.width=0;
        timg.pos(_x,_y);
        timg.rotation=_angle;
        _parent.addChild(timg);
        Laya.Tween.to(timg,{width:_len},_time,null,Laya.Handler.create(this,()=>{
            timg.removeSelf();
            Laya.Pool.recover("lineimg",timg);
            if(_func)_func();
        }))

    }

    private static getAnim(_type:string,_func:Function){
        let tanim = Laya.Pool.getItem(_type);
        if(tanim)_func(tanim);
        else{
            if(_type==this.BombAnim){
                Laya.loader.load("res/atlas/anims/expl.atlas", new Laya.Handler(this, ()=>{
                    let Animation = Laya.Animation;
                    let ani = new Animation();
                    ani.loadAtlas("res/atlas/anims/expl.atlas");
                    ani.name=_type;
                    _func(ani)
                }), null, Laya.Loader.ATLAS);
            }else if(_type==this.BombSpAnim){
                let tsp:FrameAnimmationBase=new FrameAnimmationBase("main/common_textures.png",100,100,2,3);
                tsp.name=_type;
                _func(tsp)
            }
        }
    }

    private static createTEImg(){
        return new Laya.Image("main/red.png");
    }

    private static createLineImg(){
        let timg:Laya.Image=new Laya.Image("main/lightining-blue-test.png");
        timg.anchorY=0.5;
        return timg
    }
}